package com.example.tiantian.clock;

import com.example.tiantian.R;

import android.app.AlarmManager;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

import android.widget.RemoteViews;

public class AlarmService extends Service implements OnClickListener {

    private final static String INTENT_EXTRA_ALARMTIME = "alarmTime";
    private final static long DELAY_TIME = 5000;

    static long alarmTime;
    static AlarmManager alarmMgr;

    static MediaPlayer player;

    static NotificationManager notificationMgr;

    @Override
    public void onCreate() {
        super.onCreate();

        initAudio();
        alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        notificationMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        alarmTime = intent.getLongExtra(INTENT_EXTRA_ALARMTIME, System.currentTimeMillis());
        // Intent newIntent = new Intent(this, AlarmActivity.class);
        // newIntent.putExtra(INTENT_EXTRA_ALARMTIME, alarmTime + DELAY_TIME);
        // newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        // startActivity(newIntent);

        // AlertDialog.Builder builder = new
        // AlertDialog.Builder(this).setTitle(R.string.alarm)
        // .setIcon(R.drawable.clock_icon).setPositiveButton(R.string.alarm_close,
        // this)
        // .setNegativeButton(R.string.alarm_delay, this);
        // Dialog dialog = builder.create();
        // dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        // dialog.show();

        String flag = intent.getStringExtra("flag");
        if (flag == null) {
            RemoteViews views = new RemoteViews(getPackageName(), R.layout.notification_alarm);

            Intent delayIntent = new Intent(this, AlarmService.class);
            delayIntent.putExtra(INTENT_EXTRA_ALARMTIME, alarmTime);
            delayIntent.putExtra("flag", "delay");
            PendingIntent delayPendingIntent = PendingIntent.getService(this, 101, delayIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT);
            views.setOnClickPendingIntent(R.id.alarm_delay, delayPendingIntent);

            Intent closeIntent = new Intent(this, AlarmService.class);
            closeIntent.putExtra("flag", "close");
            PendingIntent closePendingIntent = PendingIntent.getService(this, 102, closeIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT);
            views.setOnClickPendingIntent(R.id.alarm_close, closePendingIntent);

            // NotificationCompat.Builder builder = new
            // NotificationCompat.Builder(this);
            Notification.Builder newBulider = new Notification.Builder(this);

            // builder.setSmallIcon(R.drawable.clock_icon).setTicker("����").setAutoCancel(true).setOngoing(true)
            // .setDefaults(Notification.DEFAULT_ALL).setContent(views);

            newBulider.setSmallIcon(R.drawable.clock_icon).setTicker("����").setAutoCancel(true).setOngoing(true)
                    .setDefaults(Notification.DEFAULT_ALL).setContent(views);

            // notificationMgr.notify(1, builder.build());
            notificationMgr.notify(1, newBulider.build());
        } else {
            if (flag.equals("delay")) {
                Intent newIntent = new Intent(this, AlarmService.class);
                intent.putExtra(INTENT_EXTRA_ALARMTIME, alarmTime + DELAY_TIME);
                PendingIntent operation = PendingIntent.getService(this, 100, newIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                alarmMgr.set(AlarmManager.RTC_WAKEUP, alarmTime + DELAY_TIME, operation);
            }
            stopSelf();
            notificationMgr.cancel(1);
        }
        return super.onStartCommand(intent, flags, startId);
    }

    private void initAudio() {
        player = MediaPlayer.create(this, R.raw.maid_with_the_flaxen_hair);
        player.setLooping(true);
        player.start();
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (which == Dialog.BUTTON_NEGATIVE) {
            Intent intent = new Intent(this, AlarmService.class);
            intent.putExtra(INTENT_EXTRA_ALARMTIME, alarmTime + DELAY_TIME);
            PendingIntent operation = PendingIntent.getService(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            alarmMgr.set(AlarmManager.RTC_WAKEUP, alarmTime + DELAY_TIME, operation);
        }
        stopSelf();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (player != null) {
            player.stop();
            player.release();
            player = null;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}
